﻿using Newtonsoft.Json.Linq;
using StartEAS_API.Utils;
using StartsEAS_API.DAL.DALTools;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.DAL.Student
{
    public class StudentDAL
    {
        private readonly StarsEASContext _context;
        public StudentDAL(StarsEASContext context)
        {
            _context = context;
        }

        public ResultUtils InsertStudent(Model.Models.Student data)
        {
            StarsEASContext _context = new StarsEASContext();
            if (data == null) {
                AppLog.WriteError($"添加学生信息---学生信息为空", true);
                return new ResultUtils() { code = 500, message = "学生信息有误" };
            }
            int count = _context.Students.Where<Model.Models.Student>(c => c.ClassId == data.ClassId).Count()+1;
            data.Id = 0;
            data.StuId = data.ClassId +( count > 9 ? count : "0" + count);
            AppLog.WriteInfo($"准备添加学生信息：{data.Id}-{data.StuId}-{data.StuName}-{data.StuAge}-{data.StuSex}-{data.StuCarid}-{data.ClassId}-{data.StubackTime}-{data.StuPhone}-{data.PoliticalLandscape}-{data.NativePlace}-{data.National}-{data.StuEmail}", true);
            _context.Students.Add(data);
            User user = new User() {
                Username = data.StuId.ToString(),
                Password = Utils.GenerateMD5(data.StuCarid.Substring(data.StuCarid.Length - 6, 6)),
                PositionId = 1
            };
            AppLog.WriteInfo($"添加学生信息---{data.StuId.ToString()}学生账户录入", true);
            _context.Users.Add(user);
            int resultCode = _context.SaveChanges();
            AppLog.WriteInfo($"{data.StuName}学生信息添加状态：{resultCode}", true);
            return new ResultUtils() {code=200,message= resultCode };
        }

        public ResultUtils GetAllStudent() {
            StarsEASContext _context = new StarsEASContext();
            List<Model.Models.Student> studentList = _context.Students.ToList();
            if (studentList == null)
            {
                AppLog.WriteError($"查询学生信息---查询结果为空", true);
                return new ResultUtils() { code = 500, message = "Error" };
            }
            AppLog.WriteInfo($"查询学生信息---共查询到{studentList.Count()}条结果", true);
            return new ResultUtils() { code = 200, message = studentList };
        }

        public ResultUtils UpdateStudent(Model.Models.Student data) {
            StarsEASContext _context = new StarsEASContext();
            if (data == null) {
                AppLog.WriteError($"修改学生信息---请输入需要修改的学生信息", true);
                return new ResultUtils() { code=500, message = "请输入需要修改的学生信息" };
            }
            Model.Models.Student student = _context.Students.Where<Model.Models.Student>(s=>s.StuId==data.StuId).FirstOrDefault();
            if (student == null) {
                AppLog.WriteError($"修改学生信息---未查询到学生信息", true);
                return new ResultUtils() { code = 500, message = "未查询到学生信息" };
            }
            student.StuName = data.StuName;
            student.StuAge = data.StuAge;
            student.StuSex = data.StuSex;
            student.StuCarid = data.StuCarid;
            student.ClassId = data.ClassId;
            student.StubackTime = data.StubackTime;
            student.StuPhone = data.StuPhone;
            student.PoliticalLandscape = data.PoliticalLandscape;
            student.NativePlace = data.NativePlace;
            student.National = data.National;
            student.StuEmail = data.StuEmail;
            student.StateOfTheSchool = data.StateOfTheSchool;
            int result = _context.SaveChanges();
            if (result == 0) {
                AppLog.WriteError($"修改学生信息---修改失败或与原数据相同", true);
                return new ResultUtils() { code = 500, message = "修改失败或与原数据相同" };
            }
            return new ResultUtils() { code = 200, message = result };
        }

        public Model.Models.Student GetStudent(string StuId)
        {
            StarsEASContext _context = new StarsEASContext();
            Model.Models.Student student = _context.Students.Where(s => s.StuId==StuId).FirstOrDefault();
            if (student == null) {
                AppLog.WriteError($"查询学生信息---{StuId}未查询到学生信息", true);
                return null;
            }
            AppLog.WriteInfo($"查询学生信息---{StuId}成功查询到学生信息", true);
            return student;
        }


        public IEnumerable<Model.Models.Student> GetStudentByClassId(string token) {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"查询学生信息---账户{token}尝试登录但登录失败", true);
                return null;
            }
            Worker worker = token.GetWorkerByToken(_context);
            Class classes = _context.Classes.FirstOrDefault(c=>c.WorkerId==worker.WorkerId);
            if (classes == null)
            {
                AppLog.WriteError($"查询学生信息---{worker.WorkerName}未绑定班级信息", true);
                return null;
            }
            IEnumerable<Model.Models.Student> stuList = _context.Students.Where(s=>s.ClassId==classes.ClassId).ToList();
            AppLog.WriteInfo($"查询学生信息---{worker.WorkerName}的班级{classes.ClassId}查询到{stuList.Count()}条学生信息", true);
            return stuList;
        }


        public IEnumerable<Model.Models.Student> GetAllStudentByClassId(string classId) {
            StarsEASContext _context = new StarsEASContext();
            if (classId == null) {
                AppLog.WriteError("查询学生信息---班级输入为空",true);
                return null;
            }
            IEnumerable<Model.Models.Student> students = _context.Students.Where(s=>s.ClassId==classId).ToList();
            if (students == null) {
                AppLog.WriteError($"查询学生信息---根据{classId}未查询到学生列表", true);
                return null;
            }
            AppLog.WriteInfo($"查询学生信息---根据{classId}查询到{students.Count()}条学生信息",true);
            return students;
        }

    }
}
